﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" charset="utf-8" src="~/Content/ueditor/ueditor.parse.min.js"></script>
<form id="form" class="layui-form" style="margin-top: 25px">
    @Html.AntiForgeryToken()

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label label-required">富文本标题</label>
            <div class="layui-input-inline">
                <input type="text" name="Name" lay-verify="required" placeholder="请输入富文本标题" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <input type="hidden" name="Id" />
            <label class="layui-form-label">内容</label>
            <div class="layui-input-inline">
                <script id="editor" name="Content" type="text/plain" style="width:750px;height:400px;">
                </script>
            </div>
        </div>
    </div>


    <div class="layui-form-item" style="display: none">
        <div class="layui-input-block">
            <button id="btnSubmit" class="layui-btn" lay-submit lay-filter="add">提交</button>
        </div>
    </div>
</form>

<script>
    function HTMLEncode(html) {
        var temp = document.createElement("div");
        (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
        var output = temp.innerHTML;
        temp = null;
        return output;
    }
    function htmlDecode(s) {
        var div = document.createElement('div');
        div.innerHTML = s;
        return div.innerText || div.textContent;
    }

    layui.use(['form'], function () {
        var form = layui.form;
        var primaryKey = $.getQueryString("primaryKey");
        $("#Id").val(primaryKey);

        var ue = UE.getEditor('editor');
        uParse('.content', {
            'liiconpath': '/Content/ueditor/themes/ueditor-list/'    //使用 '/' 开头的绝对路径
        });
        if (primaryKey) {
            $.ajax({
                url: "/Ex/RichText/GetForm",
                data: { primaryKey: primaryKey },
                type: "post",
                dataType: "json",
                async: false,
                success: function (data) { 
                    ue.addListener("ready", function () {
                        // editor准备好之后才可以使用  
                        ue.setContent(data.Content);
                    });
                    $("#form").formSerialize(data); 
                }
            });
        }

        form.render();
        form.on('submit(add)', function (data) {
            //判断文本是否有内容
            if (!ue.hasContents()) {
                $.layerMsg("请输入内容！", 0);
                return;
            }
            data.field.Content = HTMLEncode(data.field.Content);
            //获得文本内容
            $.formSubmit({
                url: "/Ex/RichText/Form",
                data: data.field
            });
            return false;
        });
    });
</script>